package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.mapper.BusinessMapper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.PartnerMapper;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.Business;
import com.heima.pojo.Node;
import com.heima.pojo.User;
import com.heima.pojo.VmList;
import com.heima.pojo.dto.NodeDTO;
import com.heima.pojo.dto.NodePageQueryDTO;
import com.heima.pojo.vo.NodeVo;
import com.heima.pojo.vo.RegionVo;
import com.heima.pojo.vo.TypeVo;
import com.heima.pojo.vo.VmListVo;
import com.heima.service.NodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class NodeServiceImpl implements NodeService {

    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private BusinessMapper businessMapper;


    /**
     * 点位搜索以及点位详细展示
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public PageResult regionQuery(NodePageQueryDTO dto) {
        dto.check();
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());

        //获取点位的信息
        Page<NodeVo> page = nodeMapper.nodeQuery(dto);

        //获取所以运营区域的信息
        List<RegionVo> regionVoList = regionMapper.regionALl();

        //获取所有商圈的信息
        List<Business> businessesList = businessMapper.businessAll();

        for (NodeVo nodeVo : page.getResult()) {
            for (RegionVo regionVo : regionVoList) {
                if (nodeVo.getRegionId().equals(regionVo.getId())) {
                    nodeVo.setRegion(regionVo);
                }
            }
            for (Business business : businessesList) {
                if (nodeVo.getBusinessId().equals(business.getId())) {
                    nodeVo.setBusinessType(business);
                }
            }
        }
        return new PageResult(dto.getPageIndex(), dto.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }


    @Override
    public Boolean save(Node node) {
        node.setCreateTime(LocalDateTime.now());
        node.setUpdateTime(LocalDateTime.now());
        int result = nodeMapper.insert(node);
        return result != 0;
    }

    @Override
    @Transactional
    public List<VmListVo> nodeFindById(Integer nodeId) {
        //查询出点位中所有的自动售货机
        List<VmListVo> vmList = nodeMapper.nodeFindById(nodeId);
        //查询自动售货机的类型
        List<TypeVo> typeVo = nodeMapper.typeFindAll();
        //查询点位详细信息
        List<NodeVo> nodeVo = nodeMapper.nodeFindAll();
        //获取所以运营区域的信息
        List<RegionVo> regionVoList = regionMapper.regionALl();
        //获取所有商圈的信息
        List<Business> businessesList = businessMapper.businessAll();


        for (VmListVo vm : vmList) {
            for (TypeVo type : typeVo) {
                if (vm.getVmType().equals(type.getTypeId())){
                    vm.setType(type);
                }
            }
            for (NodeVo node : nodeVo) {
                for (RegionVo region : regionVoList) {
                    if (node.getRegionId().equals(region.getId())) {
                        node.setRegion(region);
                    }
                }
                for (Business business : businessesList) {
                    if (node.getBusinessId().equals(business.getId())) {
                        node.setBusinessType(business);
                    }
                }
                if (vm.getNodeId().equals(node.getId())){
                    vm.setNode(node);
                }
            }
            for (RegionVo region: regionVoList) {
                if (vm.getRegionId().equals(region.getId())){
                    vm.setRegion(region);
                }
            }
        }

        return vmList;
    }

    @Override
    public Boolean updateNode(Integer nodeId, NodeDTO dto) {
        Node node = new Node();
        BeanUtils.copyProperties(dto,node);
        node.setUpdateTime(LocalDateTime.now());
        node.setId(nodeId);
        int result = nodeMapper.updateById(node);
        return result != 0;
    }

    @Override
    public Boolean deleteById(Integer nodeId) {
        int result = nodeMapper.deleteById(nodeId);
        return result != 0;
    }

    @Override
    public Integer nodeCount() {
        return  nodeMapper.nodeCount();
    }


}
